define(["jquery","bbcare",
	"fu/app/question/question.servers"],function($,$bb,$questionServers){
	var servers = {};

    /**
     * 获取配置数据库时的参数 队列id
     */
    var getPageParams = function(){
        return $bb.getPageParams();
    }
    servers.getPageParams = getPageParams;

    /**
	 * 今日门诊记录
	 */
	servers.right = function(options){
        var data = {};
        data.list = [];
		data.show = options.list && options.list.length > 0 ? true : false;
		if( options.list && options.list.length > 0 ){
			data.list = options.list.reverse();
		}
		return data;
	}

	/**
	 * 遍历删除静态题型 (倒序循环是为了删除)
	 */
	var bfs = function(question){
		for (var i = question.length - 1; i >= 0; i--) {
			if( question[i].children ){
				question[i].children = bfs(question[i].children);
			}else{
				switch(question[i].type){
					case "question_staticText":
					case "question_staticFormat":
					case "question_staticAge":
					case "question_staticAtualAge":
					case "question_staticGestationalAge":
					case "question_staticTextarea":
						question.splice(i,1);
						break;
				}
			}
		}
		return question;
	}

	/**
	 * 离线时，问卷加建档的必填题目
	 */
	var defaultQuestion = function(){
		var arr = [{
                "id": "Pub1100001",
                "label": "手机号码",
                "length": 6,
                "placeHolder": "",
                "type": "question_autocomplete",
                "validate": [
                    {
                        "rule": "numberStr",
                        "msg": "手机号码请输入数字"
                    },
                    {
                        "rule": "length",
                        "val": 11,
                        "msg": "请输入11位手机号码"
                    },
                    {
                        "rule": "phone",
                        "msg": "请输入正确的手机号码！"
                    }
                ],
                "attention": [
                    {
                        "rule": "noNull"
                    }
                ]
            },
            {
                "id": "Pub1100002",
                "label": "患者姓名",
                "length": 6,
                "placeHolder": "",
                "validate":[
                    {
                        "rule":"noNull",
                        "msg":"请输入患者姓名！"
                    }              
                ],
                "attention": [
                    {
                        "rule": "noNull"
                    }
                ],
                "type": "question_autocomplete"
            },
            {
                "id": "Pub1100003",
                "label": "性别",
                "length": 6,
                "list": [
                    "男",
                    "女"
                ],
                "val": "男",
                "validate":[
                    {
                        "rule":"noNull",
                        "msg":"请选择性别！"
                    }              
                ],
                "attention": [
                    {
                        "rule": "noNull"
                    }
                ],
                "type": "question_radio"
            },
            {
                "id": "Pub1100004",
                "label": "出生日期",
                "length": 6,
                "validate":[
                    {
                        "rule":"noNull",
                        "msg":"请输入出生日期！"
                    }              
                ],
                "attention": [
                    {
                        "rule": "noNull"
                    }
                ],
                "type": "question_date"
            },
            {
                "id": "Pub12x00007",
                "label": "诊疗卡号",
                "length": 6,
                "placeHolder": "",
                "type": "question_autocomplete",
                "attention": [
                    {
                        "rule": "noNull"
                    }
                ]
            },
            {
                "id": "Pub12x00008",
                "label": "住院号",
                "length": 6,
                "placeHolder": "",
                "type": "question_autocomplete"
            }
        ];
        return arr;
	}	

    /**
     * 获取问卷 处理问卷
     */
    servers.getQuestions = function(templId,seqNo){
    	var data = JSON.parse( $bb.getIocalStorage("templId" + templId) );
		for (var i = 0; i < data.questiones.length; i++) {
			data.questiones[i] = bfs(data.questiones[i]); // 删除静态题型
		}
		// 加必填题目
		if( seqNo != "0" && data.questiones[0] && data.questiones[0][0] && data.questiones[0][0].children ){
			data.questiones[0][0].children = data.questiones[0][0].children.concat(defaultQuestion());
		}
		return data;
    }

    /**
     * 获取建档、修改门诊任务时的保存参数
     */
    servers.getQuestionParams = function(params){
        var sessinonUserName = JSON.parse($bb.getIocalStorage("cacheNav")).alias;
        var data = getPageParams();
        var patientName = $("#specialNurseContent > form").find("input[name='Pub1100002']").val();
        var formId = $("#specialNurseContent > form").attr("id");
        var questiones = $questionServers.getDataForDom(formId);
        var info = {
            "patientName":patientName,
            "queueName":data.menuName,
            "taskId":params.taskId,
            "seqNo":params.seqNo,
            "taskName":params.taskName,
            "templId":params.templId
        };
        if( params.seqNo == "0" ){
            $.extend(info,{"type":0});// 建档
            var paramsSave = {
                "followQueueId": data.queueId,
                "opCode": "1000",
                "opType": "2",
                "sourceType":"2",
                "userType": "1",
                "opNote": "【"+sessinonUserName + "】医生添加患者【" + patientName + "】建档信息，加入队列" + data.menuName,
                "questiones": questiones,
                "schemeList": [
                    {
                        "schemeId":data.schemeId,
                        "schemeBusiType":Number(data.schemeBusiType)
                    }
                ]
            };
        }else{
            $.extend(info,{"type":1});// task
            var paramsSave = {
                "oprInfo":{
                    "opCode":"1001",
                    "opNote":"【"+ sessinonUserName + "】医生为【" + patientName + "】患者进行门诊诊疗录入", 
                    "opType":"2",
                    "schemeBusiType":"1",
                    "followQueueId":data.queueId,
                },
                "questiones":questiones,
                "task":{
                    "taskId":params.taskId,
                }
            };
        }
        return {
            "params":paramsSave,
            "info":info
        };
    }
    
    return servers;
});
